package com.wei.miaosha.rabbitmq;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


/**
 * Created by Weizichen on 2018/9/8.
 * desc:配置类
 */
@Configuration
public class MQconfig {
    public static final String QUEUE = "queue";
    public static final String MIAOSHA_QUEUE = "miaosha.queue";
    public static final String TOPIC_QUEUE1 = "topic .queue1";
    public static final String TOPIC_QUEUE2 = "topic .queue2";
    public static final String HEADER_QUEUE = "header.queue";
    public static final String TOPIC_EXCHAGE = "topicExchage";
    public static final String ROUTING_KEY1 = "topic.key1";
    public static final String ROUTING_KEY2 = "topic.#";
    public static final String FANOUT_EXCHAGE = "fanoutExchage";
    public static final String HEADERS_EXCHANGE = "headersExchage";

    /**
     * Direct模式 交换机Exchange
     */
    @Bean
    public Queue queue1() {
        return new Queue(QUEUE, true); // true表示持久化该队列
    }

    /**
     * Topic模式 交换机Exchange
     */
    @Bean
    public Queue topicQueue1() {

        return new Queue(TOPIC_QUEUE1, true); // true表示持久化该队列
    }

    @Bean
    public Queue topicQueue2() {

        return new Queue(TOPIC_QUEUE2, true);
    }

    //声明交互器
    @Bean
    TopicExchange topicExchange() {

        return new TopicExchange(TOPIC_EXCHAGE);
    }

    //绑定
    @Bean
    public Binding binding1() {

        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(ROUTING_KEY1);
    }

    @Bean
    public Binding binding2() {

        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ROUTING_KEY2);
    }

    /**
     * Fanout模式 交换机Exchange
     */

//声明交互器
    @Bean
    FanoutExchange FanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHAGE);
    }

    //绑定
    @Bean
    public Binding FanoutBinding1() {
        return BindingBuilder.bind(topicQueue1()).to(FanoutExchange());
    }

    @Bean
    public Binding FanoutBinding2() {
        return BindingBuilder.bind(topicQueue2()).to(FanoutExchange());
    }

    /**
     * Header模式 交换机Exchange
     */

    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange(HEADERS_EXCHANGE);
    }

    @Bean
    public Queue headerQueue1() {
        return new Queue(HEADER_QUEUE, true); // true表示持久化该队列
    }

    @Bean
    public Binding headerBinding() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Header1", "Value1");
        map.put("Header2", "Value2");
        return BindingBuilder.bind(headerQueue1()).to(headersExchange()).whereAll(map).match();
    }

    /**
     * Direct模式 交换机Exchange,miaosha用
     */
    @Bean
    public Queue queueMiaoSha() {
        return new Queue(MIAOSHA_QUEUE, true); // true表示持久化该队列
    }

}
